<?php
  include "include/config.inc.php";
  $pendingInstallmentArray=array();
  $today = date('Y-m-d');
  $i=0;
  if(isset($_POST['selectPandingOrAll']) && $_POST['selectPandingOrAll'] == 'allCards')
  {
  	$selectDrawnstallmentNo = "SELECT loanDrawId
                                 FROM drawinstallment
                             GROUP BY loanDrawId" ;
    $selectDrawnstallmentNoRes = mysql_query($selectDrawnstallmentNo);
    while($selectDrawnstallmentNoRow = mysql_fetch_array($selectDrawnstallmentNoRes))
    {
      $selLastResieveDate = "SELECT MAX(receiveDate) AS lastResieveDate
                               FROM drawinstallment 
                              WHERE loanDrawId =".$selectDrawnstallmentNoRow['loanDrawId'];
      $selLastResieveDateRes = mysql_query($selLastResieveDate);
      $selLastResieveDateRow = mysql_fetch_array($selLastResieveDateRes);
      
      $selPendingInstallment = "SELECT count(*) AS pendingInstallment
                                  FROM drawinstallment
                                 WHERE drawInstallmentDate < '".$today."'
                                   AND loanDrawId =".$selectDrawnstallmentNoRow['loanDrawId']."
                                   AND isReceived = 'N'";
      $selPendingInstallmentRes = mysql_query($selPendingInstallment); 
      $selPendingInstallmentRow = mysql_fetch_array($selPendingInstallmentRes);
      if($selPendingInstallmentRow['pendingInstallment'] > 0)
      {
        $pendingInstallmentArray[$i]['loanDrawId']    = $selectDrawnstallmentNoRow['loanDrawId'];
        $pendingInstallmentArray[$i]['lastResieveDate']    = date('d-m-Y',strtotime($selLastResieveDateRow['lastResieveDate']));
        $pendingInstallmentArray[$i]['pendingInstallment'] = $selPendingInstallmentRow['pendingInstallment'];
        $i++;
      }
    }
  }
  else
  {
    $selectDrawnstallmentNo = "SELECT loanDrawId
                                 FROM drawinstallment 
                                WHERE drawInstallmentDate < '".$today."'
                                  AND isReceived = 'N'
                             GROUP BY loanDrawId" ;
    $selectDrawnstallmentNoRes = mysql_query($selectDrawnstallmentNo);
    
    while($selectDrawnstallmentNoRow = mysql_fetch_array($selectDrawnstallmentNoRes))
    {
      $selLastResieveDate = "SELECT MAX(receiveDate) AS lastResieveDate
                               FROM drawinstallment 
                              WHERE loanDrawId =".$selectDrawnstallmentNoRow['loanDrawId'];
      $selLastResieveDateRes = mysql_query($selLastResieveDate);
      $selLastResieveDateRow = mysql_fetch_array($selLastResieveDateRes);
      $selPendingInstallment = "SELECT count(*) AS pendingInstallment
                                  FROM drawinstallment
                                 WHERE drawInstallmentDate < '".$today."'
                                   AND loanDrawId =".$selectDrawnstallmentNoRow['loanDrawId']."
                                   AND isReceived = 'N'";
      $selPendingInstallmentRes = mysql_query($selPendingInstallment); 
      $selPendingInstallmentRow = mysql_fetch_array($selPendingInstallmentRes);
      if($selPendingInstallmentRow['pendingInstallment'] > 0)
      {
        $pendingInstallmentArray[$i]['loanDrawId']    = $selectDrawnstallmentNoRow['loanDrawId'];
        $pendingInstallmentArray[$i]['lastResieveDate']    = date('d-m-Y',strtotime($selLastResieveDateRow['lastResieveDate']));
        $pendingInstallmentArray[$i]['pendingInstallment'] = $selPendingInstallmentRow['pendingInstallment'];
        $i++;
      }
    }
  }
  
  $smarty->assign('pendingInstallmentArray',$pendingInstallmentArray);
  $smarty->display('PendingInstallment.tpl');
?>